(:
 : Copyright 2010 28msec Inc.
 :)

module namespace guestbook = "http://www.28msec.com/templates/guestbook/guestbook";

import module namespace http = "http://www.28msec.com/modules/http";

import module namespace xqddf = "http://www.zorba-xquery.com/modules/xqddf";

declare ordered collection guestbook:entries as node()*;
declare variable $guestbook:entries as xs:QName := xs:QName("guestbook:entries");

declare sequential function guestbook:add()
{
  http:set-header("Cache-Control", "no-cache");
  xqddf:insert-nodes-last(
      $guestbook:entries,
      <entry author="{http:get-parameters("author")}" date="{fn:current-date()}" time="{fn:current-time()}">
      {
        http:get-parameters("text")
      }
      </entry>
  );
  exit returning guestbook:list();
};

declare function guestbook:list() {
  http:set-header("Cache-Control", "no-cache"),
  
  let $entries := xqddf:collection($guestbook:entries)
  let $num_entries := fn:count($entries)
  return 
    if($num_entries = 0)
    then    
      <div style="color:green;" class="entry"><b>No entries, yet.</b></div>
    else
      for $entry at $pos in $entries[position() gt $num_entries - 5]
      let $numbering := if ($num_entries lt 5) then $pos else $num_entries - ( 5 - $pos) 
      return 
                <div class="entry">
                    <b style="color:green;">{fn:data($entry/@date)} -- {fn:data($entry/@time)}</b>{" "}<b>{fn:data($entry/@author)}</b><br/>
                    <span style="float:left;color: #aaaaaa;">{ $numbering  }</span>
                    {$entry/text()}
                </div>
};
